/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：DtfMessageRouterImpl.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.cneport.transfer.message.Message;
import com.cneport.transfer.router.model.Node;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.model.RouterMessage;
import com.cneport.transfer.router.model.RouterType;
import com.cneport.transfer.router.model.User;
import com.cneport.transfer.router.support.service.RouterService;
import com.cneport.transfer.router.support.service.UserService;
import com.cneport.transfer.util.UtilTools;

@Component
public class DtfMessageRouterImpl implements MessageRouter {

    @Autowired
    private UserService userService;

    @Autowired
    private RouterService routerService;

    @Value("${Server.Local.SenderId}")
    private String pNodeId;

    private String senderNodeId;
    private String receiverClientId;
    private String receiverNodeId;
    private String senderClientId;

    public String getSenderNodeId() {
        return senderNodeId;
    }

    public void setSenderNodeId(String senderNodeId) {
        this.senderNodeId = senderNodeId;
    }

    public String getReceiverClientId() {
        return receiverClientId;
    }

    public void setReceiverClientId(String receiverClientId) {
        this.receiverClientId = receiverClientId;
    }

    public String getReceiverNodeId() {
        return receiverNodeId;
    }

    public void setReceiverNodeId(String receiverNodeId) {
        this.receiverNodeId = receiverNodeId;
    }

    public String getSenderClientId() {
        return senderClientId;
    }

    public void setSenderClientId(String senderClientId) {
        this.senderClientId = senderClientId;
    }

    public RouterMessage route(Message<?> message) {

        RouterMessage routerMessage = new RouterMessage();
        String messageReceiverId = message.getReceiverId();
        String messageSenderId = message.getSenderId();
        String[] receiverIdAndNode = messageReceiverId.split("@");
        setReceiverClientId(receiverIdAndNode[0]);
        setReceiverNodeId(receiverIdAndNode[1]);
        String[] senderIdAndNode = messageSenderId.split("@");
        setSenderClientId(senderIdAndNode[0]);
        setSenderNodeId(senderIdAndNode[1]);
        if (senderNodeId.equals(pNodeId)) {
            if (!validateClient(senderClientId)&&!senderClientId.equals("system")) {
                throw new SenderIsNotExistException(senderClientId);
            }
        }

        if (receiverNodeId.equals(pNodeId)) {
            if (!validateClient(receiverClientId)) {
                throw new ReceiverIsNotExistException(receiverClientId);
            } else {
                User user = new User();
                user.setClientId(receiverClientId);
                routerMessage.setRouterType(RouterType.user);
                routerMessage.setUser(user);
                routerMessage.setReceiverId(message.getReceiverId());
                routerMessage.setSenderId(message.getSenderId());
                return routerMessage;
            }
        } else {
            Router router = new Router();
            router.setStartNode(pNodeId);
            router.setEndNode(receiverNodeId);
            String nextNode = routerService.queryExpression(router);
            if (UtilTools.isEmptyOrNull(nextNode)) {
                throw new NodeIsNotExistException();
            }
            Node node = new Node();
            node.setNodeId(nextNode);
            routerMessage.setRouterType(RouterType.node);
            routerMessage.setNode(node);
            routerMessage.setReceiverId(message.getReceiverId());
            routerMessage.setSenderId(message.getSenderId());
            return routerMessage;
        }

    }

    private boolean validateClient(String clientId) {
        User user = userService.queryUserService(clientId);
        if (user != null) {
            return true;
        }
        return false;
    }
}
